package com.se.global;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Enumeration;
import java.util.Vector;

public class AuthHeaderRequestWrapper extends HttpServletRequestWrapper {
	public static final String USER_ID_HEADER_NAME = "user-id";
	
	private final Vector<String> userIdVector = new Vector<>();
	private final Vector<String> adminIdVector = new Vector<>();
	/**
	 * Constructs a request object wrapping the given request.
	 *
	 * @param request The request to wrap
	 * @throws IllegalArgumentException if the request is null
	 */
	public AuthHeaderRequestWrapper(HttpServletRequest request, Long userId, Long adminId) {
		super(request);
		if (userId != null){
			userIdVector.add(userId.toString());
		}else {
			userIdVector.add(null);
		}
		
		if(adminId != null){
			adminIdVector.add(adminId.toString());
		}else {
			adminIdVector.add(null);
		}
	}
	public AuthHeaderRequestWrapper(HttpServletRequest request, Long userId) {
		this(request, userId, null);
	}
	
	@Override
	public Enumeration<String> getHeaders(String name) {
		switch (name){
			case AuthHeaderRequestWrapper.USER_ID_HEADER_NAME -> {
				return this.userIdVector.elements();
			}
			case "admin-id" -> {
				return this.adminIdVector.elements();
			}
			default -> {
				return super.getHeaders(name);
			}
		}
	}
	
	@Override
	public String getHeader(String name) {
		switch (name){
			case "user-id" -> {
				return this.userIdVector.firstElement();
			}
			case "admin-id" -> {
				return this.adminIdVector.firstElement();
			}
			default -> {
				return super.getHeader(name);
			}
		}
	}
}